package com.atguigu.springcloud.config;

import com.alibaba.fastjson.JSON;
import com.atguigu.springcloud.dto.Route;
import com.atguigu.springcloud.utils.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import reactor.core.publisher.Flux;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 通过配置类实现
 * @date 2021/9/18 14:32
 */
@Configuration
public class GateWayConfig {

//    private final static Logger log = LoggerFactory.getLogger(GateWayConfig.class);
//
//    private final static ResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver();
//
//    private final static String SERVER_PREFIX = "lb://";
//
//    private Flux<RouteDefinition> routes;
//
//
//    private List<RouteDefinition> routeDefinitions;
//
//    private  List<Route> list;
//
//
//    @PostConstruct
//    private void refreshRoute(){
//        log.info("加载路由");
//
//        //初始化路由信息
//        getResources((str) ->{
//            list = JSON.parseArray(str, Route.class);
//            log.info("路由信息: {}",list.toString());
//
//        },"classpath:route.json");
//    }
//
//
//    /**
//     * @description: 获取资源
//     * @param
//     * @return
//     */
//    private void getResources(Consumer<String> consumer,String...paths){
//        for (String path : paths) {
//            Resource resources = resourceLoader.getResource(path);
//
//            InputStream inputStream = null;
//            try {
//                inputStream = resources.getInputStream();
//                consumer.accept(IOUtils.toString(inputStream,"utf-8"));
//
//                return;
//            } catch (IOException e) {
//                log.warn("加载路由文件异常{}",e.getMessage());
//                e.printStackTrace();
//            } finally {
//                FileUtils.close(inputStream);
//            }
//
//        }
//    }

//    @Bean
//    public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder) {
//        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
//        //通过访问/guonei，会转发到uri中
////        routes.route("path_route_atguigu1",
////                r -> r.path("/payment/lb/**")
////                        .uri("http://127.0.0.1:8001")).build();
//
//        list.forEach(str -> {
//            routes.route(str.getId(),
//                    r -> r.path(str.getPathPrefix())
//                            .uri(str.getAimUrlPrefix())).build();
//        });
//
//        return routes.build();
//
//    }

}
